<?php
namespace App\Services\Com;
use App\Models\Common\Process;
use App\Models\Member\Member;
use Illuminate\Support\Facades\DB;

/**
 * Created by PhpStorm.
 * User: yzy
 * Date: 2019/6/26
 * Time: 19:57
 */
class ProcessService
{
    public static function check($text, $type, $uid='', $eventId='', $bt_id=''){
        try {
            $access_key = config('process.ACCESS_KEY');

            switch ($type){
                case 1:
                    $eventId = empty($eventId) ? 'dynamic' : $eventId;
                    $url = config('process.TEXT_URL');

                    $payload = array(
                        'accessKey' => $access_key,
                        'appId' => 'default',
                        'eventId' => $eventId,
                        'type' => 'TEXTRISK',
                        'data' => array(
                            'text' => $text,
                            'tokenId' => $uid,
                        ),
                    );

                    $res = UtilService::curlPost($url, $payload);
                    if($res['code'] != 1100) throw new \Exception($res['message']);
                    if($res['riskLevel'] != 'PASS') throw new \Exception($res['riskDescription'].'_'.$res['requestId']);
                    break;

                case 2:
                    $eventId = empty($eventId) ? 'dynamic' : $eventId;
                    $url = config('process.IMG_URL');

                    $payload = array(
                        'accessKey' => $access_key,
                        'type' => 'POLITY_EROTIC_VIOLENT_ADVERT_QRCODE_IMGTEXTRISK',
                        'appId' => 'default',
                        'eventId' => $eventId,
                        'data' => array(
                            'img' => base64_encode(file_get_contents($text)),
                            'tokenId' => $uid,
                        ),
                    );

                    $res = UtilService::curlPost($url, $payload);
                    if($res['code'] != 1100) throw new \Exception($res['message']);
                    if($res['riskLevel'] == 'REJECT') throw new \Exception($res['detail']['description'].'_'.$res['requestId']);
                    break;
                case 3:
                    $video = UtilService::getVideoUrl($text);
                    $url = config('process.VIDEO_URL');

                    $payload = array(
                        'accessKey' => $access_key,
                        'imgType' => 'POLITY_EROTIC_VIOLENT_ADVERT_QRCODE_IMGTEXTRISK',
                        'audioType' => 'POLITICS_PORN_AD_MOAN_ABUSE',
                        'appId' => 'default',
                        'eventId' => 'dynamic',
                        'btId' => $uid,
//                        'callback' => 'https://jsonplaceholder.typicode.com/posts/',
                        'callback' => '',
                        'data' => array(
                            'url' => $video['playURL'],
                        ),
                    );

                    $res = UtilService::curlPost($url, $payload);
//                    if($res['code'] != 1100) throw new \Exception($res['message']);
                    if(isset($res['riskLevel']) && $res['riskLevel'] == 'REJECT') throw new \Exception($res['detail']['description'].'_'.$res['requestId']);
                    break;
                default:
                    throw new \Exception('类型错误~');
                    break;
            }
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }

        return true;
    }

    public static function add($service_uuid, $member_uuid, $place, $processData = [], $msg = ''){
        if(empty($processData)) return;

        try {
            DB::beginTransaction();

            $process = Process::create([
                'service_uuid' => $service_uuid,
                'member_uuid' => $member_uuid,
                'place' => $place,
                'msg' => $msg,
            ]);

            foreach ($processData as $datum){
                \App\Models\Common\ProcessService::create([
                    'process_uuid' => $process->uuid,
                    'content' => $datum['content'],
                    'type' => $datum['type'],
                ]);
            }

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
        }

    }

    //监控数据处理
    public static function processData($type, $service, $uuid='', $member_uuid=''){
        $processData = [];

        $member = Member::where('uuid', $member_uuid)->first();
        if($member && in_array($member->mobile, config('process.mobile'))) return $processData;

        switch ($type){
            case 2:  //投票
                if(isset($service['title']) && !empty($service['title'])) {
                    self::check($service['title'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['title'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['content']) && !empty($service['content'])) {
                    self::check($service['content'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['content'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['bg_img']) && !empty($service['bg_img'])) {
//                    self::check($service['bg_img'], 2, $uuid);
                    $processData[] = [
                        'content' =>$service['bg_img'],
                        'type' => 2,
                    ];
                }
                break;

            case 3:  //活动
                if(isset($service['name']) && !empty($service['name'])) {
                    self::check($service['name'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['name'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['content']) && !empty($service['content'])) {
                    self::check($service['content'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['content'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['head_img']) && !empty($service['head_img'])) {
//                    self::check($service['head_img'], 2, $uuid);
                    $processData[] = [
                        'content' =>$service['head_img'],
                        'type' => 2,
                    ];
                }

                if(isset($service['files']) && !empty($service['files'])) {
                    foreach ($service['files'] as $file) {
                        if(isset($file['path']) && !empty($file['path'])) {
//                            self::check($file['path'], 2, $uuid);
                            $processData[] = [
                                'content' =>$file['path'],
                                'type' => 2,
                            ];
                        }
                    }
                }

                if(isset($service['tickets']) && !empty($service['tickets'])) {
                    foreach ($service['tickets'] as $ticket) {
                        if(isset($ticket['name']) && !empty(isset($ticket['name']))){
                            self::check($ticket['name'], 1, $uuid);
//                            $processData[] = [
//                                'content' =>$ticket['name'],
//                                'type' => 1,
//                            ];
                        }
                        if(isset($ticket['content']) && !empty(isset($ticket['content']))){
                            self::check($ticket['content'], 1, $uuid);
//                            $processData[] = [
//                                'content' =>$ticket['content'],
//                                'type' => 1,
//                            ];
                        }
                    }
                }

                break;

            case 4:  //店铺
                if(isset($service['title']) && !empty($service['title'])) {
                    self::check($service['title'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['title'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['head_img']) && !empty($service['head_img'])) {
//                    self::check($service['head_img'], 2, $uuid);
                    $processData[] = [
                        'content' =>$service['head_img'],
                        'type' => 2,
                    ];
                }

                if(isset($service['content']) && !empty($service['content'])) {
                    self::check($service['content'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['content'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['files']) && !empty($service['files'])) {
                    foreach ($service['files'] as $file){
//                        self::check($file['path'], 2, $uuid);
                        $processData[] = [
                            'content' =>isset($file['path']) && !empty($file['path']) ? $file['path'] : $file,
                            'type' => 2,
                        ];
                    }
                }

                break;

            case 5:  //推文
                if(isset($service['content']) && !empty($service['content'])) {
                    self::check($service['content'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['content'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['file_list']) && !empty($service['file_list'])) {
                    $file_list = json_decode($service['file_list'], true);
                    foreach ($file_list as $file){
                        $path = isset($file['path']) && !empty($file['path']) ? $file['path'] : $file;
                        if($service['file_type'] == 'image'){
//                            self::check($path, 2, $uuid);
                            $processData[] = [
                                'content' =>$path.'?x-oss-process=image/auto-orient,1/resize,m_lfit,w_500/quality,q_100',
                                'type' => 2,
                            ];
                        }else{
                            $processData[] = [
                                'content' =>$path,
                                'type' => 3,
                            ];
                        }
                    }
                }

                if(isset($service['tags']) && !empty($service['tags'])) {
                    $param_tags_arr = json_decode($service['tags'], true);
                    foreach ($param_tags_arr as $item){
                        self::check($item, 1, $uuid);
//                        $processData[] = [
//                            'content' =>$item,
//                            'type' => 1,
//                        ];
                    }
                }

                break;

            case 6:  //用户
                if(isset($service['avatar_url']) && !empty($service['avatar_url'])) {
                    self::check($service['avatar_url'], 2, $uuid);
//                    $processData[] = [
//                        'content' =>$service['avatar_url'],
//                        'type' => 2,
//                    ];
                }

                if(isset($service['nickname']) && !empty($service['nickname'])) {
                    self::check($service['nickname'], 1, $uuid);

//                    $processData[] = [
//                        'content' =>$service['nickname'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['introduction']) && !empty($service['introduction'])) {
                    self::check($service['introduction'], 1, $uuid);

//                    $processData[] = [
//                        'content' =>$service['introduction'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['title']) && !empty($service['title'])) {
                    self::check($service['title'], 1, $uuid);

//                    $processData[] = [
//                        'content' =>$service['title'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['desc']) && !empty($service['desc'])) {
                    self::check($service['desc'], 1, $uuid);

//                    $processData[] = [
//                        'content' =>$service['desc'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['background_url']) && !empty($service['background_url'])) {
                    self::check($service['background_url'], 2, $uuid);

//                    $processData[] = [
//                        'content' =>$service['background_url'],
//                        'type' => 2,
//                    ];
                }

                if(isset($service['school_subjects']) && !empty($service['school_subjects'])) {
                    foreach ($service['school_subjects'] as $school_subject){
                        self::check($school_subject['subject'], 1, $uuid);
//                        $processData[] = [
//                            'content' =>$school_subject['subject'],
//                            'type' => 1,
//                        ];
                    }
                }

                if(isset($service['works']) && !empty($service['works'])) {
                    foreach ($service['works'] as $work){
                        self::check($work['company'], 1, $uuid);
                        self::check($work['position'], 1, $uuid);
//                        $processData[] = [
//                            'content' =>$work['company'],
//                            'type' => 1,
//                        ];
//                        $processData[] = [
//                            'content' =>$work['position'],
//                            'type' => 1,
//                        ];
                    }
                }


                break;

            case 7:  //talking
                if(isset($service['group_name']) && !empty($service['group_name'])) {
                    self::check($service['group_name'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['group_name'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['group_desc']) && !empty($service['group_desc'])) {
                    self::check($service['group_desc'], 1, $uuid);
//                    $processData[] = [
//                        'content' =>$service['group_desc'],
//                        'type' => 1,
//                    ];
                }

                if(isset($service['group_icon']) && !empty($service['group_icon'])) {
//                    self::check($service['group_icon'], 2, $uuid);
                    $processData[] = [
                        'content' =>$service['group_icon'],
                        'type' => 2,
                    ];
                }

                break;

            case 8:  //学校
                if(isset($service['school_badge_img']) && !empty($service['school_badge_img'])) {
//                    self::check($service['school_badge_img'], 2, $uuid);
                    $processData[] = [
                        'content' =>$service['school_badge_img'],
                        'type' => 2,
                    ];
                }

                if(isset($service['bg_img']) && !empty($service['bg_img'])) {
//                    self::check($service['bg_img'], 2, $uuid);
                    $processData[] = [
                        'content' =>$service['bg_img'],
                        'type' => 2,
                    ];
                }

                break;
        }

        return $processData;
    }
}